package com.roc.dao.impl;
/**
 * @author FengHaiTao
 * 
 */
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.diaodan.IDiaoDanDao;
import com.roc.dao.setting.ISettingFlagDao;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.CalcuBean;
import com.roc.pojo.DiaoDanBean;
import com.roc.pojo.DuizhangBean;
import com.roc.pojo.Operators;
import com.roc.pojo.SettFlagBean;
import com.roc.pojo.SettingBean;
import com.roc.pojo.SettingItemBean;
import com.roc.service.ServiceException;
import com.roc.util.Utils;

@Repository
public class DiaoDanImpl extends BaseDaoImpl implements IDiaoDanDao {

	@Override
	public boolean diaodanApply(AuditingItem auditing,
			DiaoDanBean diaodan) throws DaoException {
		boolean result=false;
		try {
			/*******调单申请成功---记录日志*******/
			
			editSave(auditing);
			/*******调单申请成功--------**/
			editSave(diaodan);
			result=true;
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("调单申请失败");
		}
		
		return result;
	}

	@Override
	public void frozenSetting(String settid) throws DaoException {
		try {
			Session session =sessionFactory.getCurrentSession(); 
			
			String sql=" update t_transjnls_settle t set  t.freezeflag='Y' WHERE t.id= "+settid;
			SQLQuery query = session.createSQLQuery(sql);
			query.executeUpdate();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("更新失败！");
		}
	
	}
	

	@Override
	public void unfrozenSetting(String settid) throws DaoException {
		try {
			Session session =sessionFactory.getCurrentSession(); 
			
			String sql=" update t_transjnls_settle t set  t.freezeflag='N',t.settleflag='H',t.settledate=to_char(sysdate,'YYYYMMdd') WHERE t.id= "+settid;
			SQLQuery query = session.createSQLQuery(sql);
			query.executeUpdate();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("更新失败！");
		}
	
	}
	@Override
	public DiaoDanBean queryDiaoDanBean(String id) throws DaoException {
		DiaoDanBean result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from DiaoDanBean m where m.id = :id");
			query.setString("id", id);
			result = (DiaoDanBean) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询SettingBean不唯一", nure);
			throw new DaoException("查询记录不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询出错");
		}
		return result;
	}
	@Override
	public int queryblackcount(String merno) throws DaoException {
		int i =0;
		try {
			Session session =sessionFactory.getCurrentSession(); 
				String sql=" select COUNT(*) from t_trans_register t   WHERE t.merchant_no= '"+merno+"' and t.start_date >to_char(sysdate-30,'yyyymmdd') ";
				SQLQuery query = session.createSQLQuery(sql);
				
				String str= (String)query.uniqueResult().toString();
				if(str !=null){
					i = Integer.parseInt(str);
				}
				
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("更新失败！");
		}
		
		return i;
	}
	@Override
	public List<DiaoDanBean>  queryDiaodan() throws DaoException {
		List<DiaoDanBean> list = new ArrayList<DiaoDanBean>();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from DiaoDanBean d where d.status='0' and d.reversal='0' ";
			Query query=session.createQuery(hql);
			list=query.list();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询调单信息出错");
		}
		return list;
	}
	
	@Override
	public void updatediaodan(DiaoDanBean diaodan) throws DaoException {
		try {
			/*******调单修改---记录日志*******/
			
				 editUpdate(diaodan);
			
			
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("调单修改失败");
		}
		
	}
	
	@Override
	public List<SettingBean> querySettinglist(String merno) throws DaoException {
		List<SettingBean> list =  new ArrayList<SettingBean>();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "   from SettingBean t  where  t.merchantno = :merno and t.freezeflag='N' and t.settleflag in ('Y','H') and t.loadflag='0' ";
			Query query=session.createQuery(hql);
			query.setString("merno", merno);
			
			list=query.list();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询结算信息出错");
		}
		return list;
	}
	@Override
	public SettingBean querySettlist(String settid) throws DaoException {
		SettingBean result =new SettingBean();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "   from SettingBean t  where  t.id = :settid  ";
			Query query=session.createQuery(hql);
			query.setString("settid", settid);
			result = (SettingBean) query.uniqueResult();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询结算信息出错");
		}
		return result;
	}
	
}
